package my.netty.channel.nio;

import my.netty.channel.AbstractChannel;
import my.netty.channel.EventLoop;
import my.netty.exception.ChannelException;

import java.nio.channels.SelectionKey;

public abstract class AbstractNioSocketChannel extends AbstractChannel {

    protected void register(EventLoop eventLoop, int ops) {
        this.eventLoop = eventLoop;
        NioEventLoop nioEventLoop = (NioEventLoop) eventLoop();
        try {
            javaChannel().configureBlocking(false);
            SelectionKey key = javaChannel().register(nioEventLoop.selector(), ops);
            key.attach(this);
        } catch (Throwable e) {
            close();
            throw new ChannelException("register", e);
        }
    }

}
